# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql

# CREATE TABLE depression(id int primary key auto_increment, user_id varchar(20), name varchar(20), text_raw varchar(500), publish_time varchar(20));
class WeiboPipeline:
    def open_spider(self, spider):
        self.connect = pymysql.connect(
            host="",
            port=3306,
            user="",
            passwd="",
            database=""
        )
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        sql = "insert into depression (user_id, name, text_raw, publish_time)value('%s', '%s', '%s', '%s')"\
            %(item['user_id'], item['name'], item['text_raw'], item['publish_time'])
        self.cursor.execute(sql)
        self.connect.commit()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.connect.close()
